<?php

/**
* notes.php
*
* @version 0.1a
* @copyright 2010 by badboy for BadNova
*/

define('INSIDE'  , true);
define('INSTALL' , false);

$badnova_root_path = './';
include($badnova_root_path . 'extension.inc');
include($badnova_root_path . 'common.'.$phpEx);

$dpath = (!$user['dpath']) ? DEFAULT_SKINPATH : $user['dpath'];

$n = intval($_GET['n']);
$section = (isset($_POST['s'])) ? $_POST['s'] : 0;

// lenguaje
includeLang('notes');

// blocking non-users
if ($IsUserChecked == false)
{
	includeLang('login');
	message($lang['Login_Ok'], $lang['log_numbreg']);
}

$lang['PHP_SELF'] = "notes.$phpEx";

if ($section == 1 || $section == 2) // Edit and Add notes
{
	$time = time();
	$priority = intval($_POST['u']);
	$title = ($_POST['title']) ? mysql_escape_string(strip_tags($_POST['title'])) : $lang['NoTitle'];
	$text = ($_POST['text']) ? mysql_escape_string(strip_tags($_POST['text'])) : $lang['NoText'];

	if ($section == 1)
	{
		doquery("INSERT INTO {{table}} SET owner={$user['id']}, time=$time, priority=$priority, title='$title', text='$text'", 'notes');
		message($lang['NoteAdded'], $lang['Please_Wait'],"notes.$phpEx", '3');
	}
	elseif ($section == 2)
	{
		// pequeño query para averiguar si la nota que se edita es del propio jugador
		$id = intval($_POST['n']);
		$note_query = doquery("SELECT * FROM {{table}} WHERE id=$id AND owner=".$user['id'], 'notes');

		if (!$note_query)
		{
			error($lang['notpossiblethisway'], $lang['Notes']);
		}

		doquery("UPDATE {{table}} SET time=$time, priority=$priority, title='$title', text='$text' WHERE id=$id", 'notes');
		message($lang['NoteUpdated'], $lang['Please_Wait'], "notes.$phpEx", '3');
	}
}
elseif ($_POST) // Delete
{
	foreach ($_POST as $a => $b)
	{
		// The checkbox checked have the word "delmes" and the id
		// And each array have the "y" value
		if (preg_match('/delmes/i', $a) && $b == 'y')
		{
			$id = intval(str_replace('delmes', '', $a));
			$note_query = doquery("SELECT * FROM {{table}} WHERE id=$id AND owner={$user['id']}", 'notes');
			// check
			if ($note_query)
			{
				$deleted++;
				doquery("DELETE FROM {{table}} WHERE `id`=$id", 'notes');
			}
		}
	}

	if ($deleted)
	{
		$mes = ($deleted == 1) ? $lang['NoteDeleted'] : $lang['NoteDeleteds'];
		message($mes, $lang['Please_Wait'], "notes.$phpEx", '3');
	}
	else
	{
		header("Location: notes.$phpEx");
		exit(0);
	}
}
else // Index
{
	$action = (isset($_GET['a'])) ? $_GET['a'] : 0;

	if ($action == 1) // Make new note
	{
		// Form to make new note
		$parse = $lang;

		$parse['c_Options'] = "<option value=2 selected=selected>{$lang['Important']}</option>
			  <option value=1>{$lang['Normal']}</option>
			  <option value=0>{$lang['Unimportant']}</option>";

		$parse['cntChars'] = '0';
		$parse['TITLE'] = $lang['Createnote'];
		$parse['text'] = '';
		$parse['title'] = '';
		$parse['inputs'] = '<input type=hidden name=s value=1>';

		$page = parsetemplate(gettemplate('notes_form'), $parse);

		display($page, $lang['Notes'], false);
	}
	elseif($action == 2) // Edit
	{
		// Formulario donde se puestra la nota y se puede editar.
		$note = doquery("SELECT * FROM {{table}} WHERE owner={$user['id']} AND id=$n", 'notes', true);

		if (!$note)
		{
			message($lang['notpossiblethisway'], $lang['Error']);
		}

		$cntChars = strlen($note['text']);

		$SELECTED[$note['priority']] = ' selected="selected"';

		$parse = array_merge($note, $lang);

		$parse['c_Options'] = "<option value=2{$SELECTED[2]}>{$lang['Important']}</option>
			  <option value=1{$SELECTED[1]}>{$lang['Normal']}</option>
			  <option value=0{$SELECTED[0]}>{$lang['Unimportant']}</option>";

		$parse['cntChars'] = $cntChars;
		$parse['TITLE'] = $lang['Editnote'];
		$parse['inputs'] = '<input type="hidden" name="s" value="2">
			<input type="hidden" name="n" value="'.$note['id'].'">';

		$page = parsetemplate(gettemplate('notes_form'), $parse);

		display($page, $lang['Notes'], false);
	}
	else // Index
	{
		$notes_query = doquery("SELECT * FROM {{table}} WHERE owner={$user['id']} ORDER BY time DESC", 'notes');
		// Loop para crear la lista de notas que el jugador tiene
		$count = 0;
		$list = '';
		$parse = $lang;

		$rows = array(); // used for  {body_list/} {/body_list}

		while ($row = mysql_fetch_array($notes_query))
		{
			$count++;
			//  Put colour in the title, depending of the priority
			if ($row['priority'] == 0)
			{
				$row['color'] = 'lime'; // Important
			}
			elseif ($row['priority'] == 1)
			{
				$row['color'] = 'yellow'; // Normal
			}
			elseif ($row['priority'] == 2)
			{
				$row['color'] = 'red'; // Not relevant
			}

			// cute html
			$row['time'] = @date('Y-m-d h:i:s', $row['time']);
			$row['size'] = strlen($row['text']);
			$row['PHP_SELF'] = $lang['PHP_SELF'];
			// add this row in the rows
			$rows[] = $row;
		}

		$parse = $lang;
		$parse['body_list'] = $rows; // rows is an array

		if ($count == 0)
		{
			$parse['no_notes'] = "<tr><th colspan=4>{$lang['ThereIsNoNote']}</th>";
		}

		//fragmento de template
		$page = template_parse(gettemplate('notes'), $parse);

		display($page, $lang['Notes'], false);
	}
}

// -----------------------------------------------------------------------------------------------------------
// History version
// 0.1a - Mise au propre
?>
